﻿
Partial Class WebForm_ManageProduct_SearchDummy
    Inherits System.Web.UI.UserControl
#Region "事件"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack() Then
            Me.BindDDLYear()
            Me.BindDDLDivision()
            Me.BindDDLSignature()
            Me.BindDDLMMPP(Me.ddlMMPP)
        End If
    End Sub
    Protected Sub ddlDivision_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDivision.SelectedIndexChanged
        Me.BindDDLSignature()
    End Sub
    Protected Sub gv2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv2.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If Me.gv2.DataKeys(e.Row.RowIndex).Item("IsDummy").ToString() = "N" OrElse Me.gv2.DataKeys(e.Row.RowIndex).Item("Active").ToString() = "N" Then
                TryCast(e.Row.FindControl("btnEdit"), Button).Visible = False
                TryCast(e.Row.FindControl("btnDelete"), Button).Visible = False
            End If
        End If
    End Sub

    Protected Sub gv2_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv2.PageIndexChanging
        Me.gv2.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv2_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv2.RowCommand
        If e.CommandName = "EditDummyProduct" Then

            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Me.gv2.SelectedIndex = row.RowIndex
            Me.lblProdLevelDescription.Text = ProdLevelDescription(Me.gv2.DataKeys(row.RowIndex).Item("ProdHierarchy").ToString())
            Me.txtSapCode.Text = MyObj.GetAreaCode() & Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString()
            Me.txtSapCode.Enabled = False
            Me.txtProdHierarchy.Enabled = False
            Me.txtProdHierarchy.Text = Me.gv2.DataKeys(row.RowIndex).Item("ProdHierarchy").ToString()
            Me.txtProdDescription.Text = Me.gv2.DataKeys(row.RowIndex).Item("Description").ToString()
            Me.txtLocalDescription.Text = Me.gv2.DataKeys(row.RowIndex).Item("LocalDescription").ToString()
            MyObj.MatchItem(Me.ddlMMPP, Me.gv2.DataKeys(row.RowIndex).Item("MaterialType").ToString())
            Me.txtPAllow.Text = Me.gv2.DataKeys(row.RowIndex).Item("PAllow").ToString()
            Me.txtListP.Text = Me.gv2.DataKeys(row.RowIndex).Item("ListP").ToString()
            Me.txtSTD.Text = Me.gv2.DataKeys(row.RowIndex).Item("STD").ToString()
            Me.mvEmployee.SetActiveView(vwAddDummyProduct)
        ElseIf e.CommandName = "DeleteDummyProduct" Then
            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Dim sql As New MyStringBuilder
            Dim dat As New clsData

            '刪除Dummy Product時將ProductData的資料的Active設為N,MMPP設為07,刪除目前月份的SFReport版本的資料
            sql.Length = 0
            sql.AppendFormat("update ProductData set Active='N',MMPP='07' where SapCode='{0}'", Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from SFReport{0} where SapCode='{1}' ", DateTime.Today.ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from SO_SFReport{0} where SapCode='{1}' ", DateTime.Today.ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from PLV_SFReport{0} where SapCode='{1}' ", DateTime.Today.ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from WF_SFReport{0} where SapCode='{1}' ", DateTime.Today.ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from SFReport{0} where SapCode='{1}' ", DateTime.Today.AddMonths(1).ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from SO_SFReport{0} where SapCode='{1}' ", DateTime.Today.AddMonths(1).ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from PLV_SFReport{0} where SapCode='{1}' ", DateTime.Today.AddMonths(1).ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())
            sql.AppendFormat(";Delete from WF_SFReport{0} where SapCode='{1}' ", DateTime.Today.AddMonths(1).ToString("yyyyMM"), Me.gv2.DataKeys(row.RowIndex).Item("SapCode").ToString())

            dat.AccData(sql.ToString())

            Me.BindGV()
        End If
    End Sub
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Me.BindGV()
    End Sub

    '取消管理DummyProduct
    Protected Sub btnCancelDummyProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelDummyProduct.Click
        Me.mvEmployee.SetActiveView(vwSearch)
        Me.BindGV()
    End Sub
    '儲存DummyProduct
    Protected Sub btnSaveDummyProduct_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveDummyProduct.Click
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        'Dim MySapCode As String = Me.txtSapCode.Text.Replace("'", "''").Trim()
        Dim MySapCode As String

        MySapCode = Me.txtSapCode.Text.Replace("'", "''").Trim().Replace(MyObj.GetAreaCode(), "")

        Dim MyProductHierarchy As String = Me.txtProdHierarchy.Text.Replace("'", "''").Trim()
        Dim MyDescription As String = Me.txtProdDescription.Text.Replace("'", "''").Trim()
        Dim MyLocalDescription As String = Me.txtLocalDescription.Text.Replace("'", "''").Trim()
        Dim intValue As Integer
        Dim MyPAllow As String = Me.txtPAllow.Text.Replace("'", "''").Trim()
        If Not Integer.TryParse(MyPAllow, intValue) Then
            MyPAllow = "null"
        Else
            MyPAllow = MyPAllow * 0.01
        End If

        Dim MyListP As String = Me.txtListP.Text.Replace("'", "''").Trim()
        If Not IsNumeric(MyListP) Then
            MyListP = "null"
        Else
            MyListP = Math.Round(Convert.ToDecimal(MyListP), 2).ToString()
        End If

        Dim MySTD As String = Me.txtSTD.Text.Replace("'", "''").Trim()
        If Not IsNumeric(MySTD) Then
            MySTD = "null"
        Else
            MySTD = Math.Round(Convert.ToDecimal(MySTD), 2).ToString()
        End If

        sql.Length = 0
        sql.AppendFormat("Select * From ProductData ")
        sql.AppendFormat("Where 1=1 ")
        sql.AppendFormat("and SapCode='{0}' ", MySapCode)

        '    sql.AppendFormat("and ID<>'{0}' ", Me.SelectedID)
        sql.AppendFormat("and 1=2 ")

        '有重複資料則不新增
        If dat.GetData(sql.ToString()).Rows.Count > 0 Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("SameSapCode"))
            Return
        Else

            sql.Length = 0
            sql.AppendFormat("update ProductData ")
            sql.AppendFormat("set ")
            sql.AppendFormat("Description='{0}' ", MyDescription)
            sql.AppendFormat(",LocalDescription='{0}' ", MyLocalDescription)
            sql.AppendFormat(",MMPP='{0}' ", "03") '預設為03,活的產品
            sql.AppendFormat(",MaterialType='{0}' ", Me.ddlMMPP.SelectedValue)
            sql.AppendFormat("where SapCode='{0}' ", MySapCode)
            sql.AppendFormat(";exec BuildDummyHierarchy '{0}' ", MyProductHierarchy)
            dat.AccData(sql.ToString())

            sql.Length = 0
            sql.AppendFormat("exec SetProductDataBySapCode '{0}' ", MySapCode)
            dat.AccData(sql.ToString())

            '紀錄PAllow,ListP,STD
            sql.Length = 0
            sql.AppendFormat("Insert into ProductDummy(SapCode,PAllow,ListP,STD) ")
            sql.AppendFormat("select '{0}' ", MySapCode)
            sql.AppendFormat(",{0} ", MyPAllow)
            sql.AppendFormat(",{0} ", MyListP)
            sql.AppendFormat(",{0} ", MySTD)
            sql.AppendFormat("where not exists ")
            sql.AppendFormat("(Select top 1 * from ProductDummy where SapCode='{0}') ", MySapCode)
            sql.AppendFormat(";Update ProductDummy ")
            sql.AppendFormat("set PAllow={0} ", MyPAllow)
            sql.AppendFormat(",ListP={0} ", MyListP)
            sql.AppendFormat(",STD={0} ", MySTD)
            sql.AppendFormat("where SapCode='{0}' ", MySapCode)
            dat.AccData(sql.ToString())

            'Auto Fill
            If Me.chkAutoFill.Checked Then
                Dim monList As New List(Of String)
                For i As Integer = DateTime.Now.Month To 36
                    monList.Add(i)
                Next
                sql.Length = 0
                sql.AppendFormat("Update SFReport{0} ", DateTime.Now.ToString("yyyyMM"))
                sql.AppendFormat("set {0} ", MyObj.JoinFormat(",", "Mon{0}=" & MyPAllow, monList.ToArray()))
                sql.AppendFormat("where SapCode='{0}' ", MySapCode)
                sql.AppendFormat("and SkuType='{0}' ", "PAllow")
                sql.AppendFormat(";Update SFReport{0} ", DateTime.Now.ToString("yyyyMM"))
                sql.AppendFormat("set {0} ", MyObj.JoinFormat(",", "Mon{0}=" & MyListP, monList.ToArray()))
                sql.AppendFormat("where SapCode='{0}' ", MySapCode)
                sql.AppendFormat("and SkuType='{0}' ", "ListP")
                sql.AppendFormat(";Update SFReport{0} ", DateTime.Now.ToString("yyyyMM"))
                sql.AppendFormat("set {0} ", MyObj.JoinFormat(",", "Mon{0}=" & MySTD, monList.ToArray()))
                sql.AppendFormat("where SapCode='{0}' ", MySapCode)
                sql.AppendFormat("and SkuType='{0}' ", "STD")
                dat.AccData(sql.ToString())
            Else
                sql.Length = 0
                sql.AppendFormat("Update SFReport{0} ", DateTime.Now.ToString("yyyyMM"))
                sql.AppendFormat("set Mon{0}={1} ", DateTime.Now.Month, MyPAllow)
                sql.AppendFormat("where SapCode='{0}' ", MySapCode)
                sql.AppendFormat("and SkuType='{0}' ", "PAllow")
                sql.AppendFormat(";Update SFReport{0} ", DateTime.Now.ToString("yyyyMM"))
                sql.AppendFormat("set Mon{0}={1} ", DateTime.Now.Month, MyListP)
                sql.AppendFormat("where SapCode='{0}' ", MySapCode)
                sql.AppendFormat("and SkuType='{0}' ", "ListP")
                sql.AppendFormat(";Update SFReport{0} ", DateTime.Now.ToString("yyyyMM"))
                sql.AppendFormat("set Mon{0}={1} ", DateTime.Now.Month, MySTD)
                sql.AppendFormat("where SapCode='{0}' ", MySapCode)
                sql.AppendFormat("and SkuType='{0}' ", "STD")
                dat.AccData(sql.ToString())
            End If

            Me.mvEmployee.SetActiveView(vwSearch)
            Me.BindGV()
        End If
    End Sub
#End Region

#Region "繫結資料"
    Private Sub BindDDLDivision()
        Dim ddlControl As DropDownList = Me.ddlDivision
        Dim dvManager As New DivisionManager

        ddlControl.DataTextField = "Division"
        ddlControl.DataValueField = "Division"
        ddlControl.DataSource = dvManager.GetAllDivision()
        ddlControl.DataBind()
    End Sub

    Private Sub BindDDLSignature()
        Dim ddlControl As DropDownList = Me.ddlSignature
        Dim stManager As New SignatureManager

        ddlControl.DataTextField = "Description"
        ddlControl.DataValueField = "Signature"
        ddlControl.DataSource = stManager.GetSignatureNoNewCategories(Me.ddlDivision.SelectedValue)
        ddlControl.DataBind()
    End Sub

    Private Sub BindDDLYear()
        Dim d As Date = Date.Now.AddMonths(3)

        Me.ddlYear.Items.Clear()
        For i As Integer = d.Year To 2010 Step -1

            Dim li As New ListItem
            li.Value = d.Year
            li.Text = d.ToString("yyyy")
            Me.ddlYear.Items.Add(li)

            d = d.AddYears(-1)
        Next
        Me.ddlYear.SelectedValue = Date.Now.Year
    End Sub

    Private Sub BindDDLMMPP(ByVal ddlControl As DropDownList)
        ddlControl.DataTextField = "Text"
        ddlControl.DataValueField = "Value"
        ddlControl.DataSource = MyObj.GetCodeMappingData("Product", "MPStatus")
        ddlControl.DataBind()
        ddlControl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
        For i As Integer = ddlControl.Items.Count - 1 To 0 Step -1
            If ddlControl.Items(i).Value.IndexOf("All") <> -1 Then
                ddlControl.Items.RemoveAt(i)
            End If
        Next
    End Sub
#End Region

#Region "方法"
    Private Sub BindGV()
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.Length = 0
        sql.AppendFormat("Select distinct a.*,a.ProdHierarchy as Code,b.Text as MMPP_Text ")
        sql.AppendFormat(",Case Isnull(pd.PAllow,'') when '' then '' else pd.PAllow*100 end as PAllow ")
        sql.AppendFormat(",pd.ListP,pd.STD ")
        sql.AppendFormat("From ProductData a ")
        sql.AppendFormat("left join CodeMapping b on a.MaterialType=b.Value and b.TableName='Product' and b.ColumnName='MPStatus' ")
        sql.AppendFormat("left join ProductDummy pd on a.SapCode=pd.SapCode ")
        sql.AppendFormat("where 1=1 ")
        sql.AppendFormat("and a.SapCode like '%B{0}{1}%' ", Right(ddlYear.SelectedValue, 2), ddlSignature.SelectedValue)
        'sql.AppendFormat("and a.Active='Y' ")
        'sql.AppendFormat("order by Description ")
        sql.AppendFormat("order by Code ")

        Me.gv2.DataSource = dat.GetData(sql)
        Me.gv2.DataBind()
    End Sub

    Public Function ProdLevelDescription(ByVal ProdHierarchy As String) As String
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim dt As New DataTable
        sql.Length = 0
        sql.AppendFormat("exec BuildProductHierarchy; ")
        sql.AppendFormat("select * from ProductHierarchy ")
        sql.AppendFormat("where ProdHierarchy='{0}' ", ProdHierarchy)
        dt = dat.GetData(sql)
        MyObj.WriteLog(sql.ToString())
        Dim Signature As String = "(" & dt.Rows(0)("Signature").ToString() & ")"
        Dim Signature_Text As String = dt.Rows(0)("Signature_Text") & "|"
        Dim Brand As String = "(" & dt.Rows(0)("Brand").ToString() & ")"
        Dim Brand_Text As String = dt.Rows(0)("Brand_Text") & "|"
        Dim Sub_Brand As String = "(" & dt.Rows(0)("Sub-Brand").ToString() & ")"
        Dim Sub_Brand_Text As String = dt.Rows(0)("Sub-Brand_Text") & "|"
        Dim Reference As String = "(" & dt.Rows(0)("Reference").ToString() & ")"
        Dim Reference_Text As String = dt.Rows(0)("Reference_Text") & "|"
        Dim Nuance As String = "(" & dt.Rows(0)("Nuance").ToString() & ")"
        Dim Nuance_Text As String = dt.Rows(0)("Nuance_Text") & "|"
        Dim Axe As String = "(" & dt.Rows(0)("Axe").ToString() & ")"
        Dim Axe_Text As String = dt.Rows(0)("Axe_Text") & "|"
        Dim Sub_Axe As String = "(" & dt.Rows(0)("Sub-Axe").ToString() & ")"
        Dim Sub_Axe_Text As String = dt.Rows(0)("Sub-Axe_Text") & "|"
        Dim Classs As String = "(" & dt.Rows(0)("Class").ToString() & ")"
        Dim Class_Text As String = dt.Rows(0)("Class_Text") & "|"
        Dim Functions As String = "(" & dt.Rows(0)("Function").ToString() & ")"
        Dim Function_Text As String = dt.Rows(0)("Function_Text")

        Return Signature & Signature_Text & Brand & Brand_Text & Sub_Brand & Sub_Brand_Text & Reference & Reference_Text & Nuance & Nuance_Text & Axe & Axe_Text & Sub_Axe & Sub_Axe_Text & Classs & Class_Text & Functions & Function_Text
    End Function
#End Region
End Class
